/**
 * @program: Demo2
 * @description:
 * @author: yan guang
 * @create: 2020-04-17 15:18
 */
public class CustomerView {
    private CustomerList customerList = new CustomerList(10);

    public void enterMainMenu() {
        boolean isfalg = true;
        while (isfalg) {

            System.out.println("\n-----------------客户信息管理软件--------------");
            System.out.println("1添加客户");
            System.out.println("2修改客户");
            System.out.println("3删除客户");
            System.out.println("4客户列表");
            System.out.println("5退 出\n");
            System.out.print(" 请选择(1-5): ");

            char menu = CMUtility.readMenuSelection();

            switch (menu) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomers();
                    break;
                case '5':
                    System.out.println("确认是否退出（Y/N）");
                    char exit = CMUtility.readConfirmSelection();
                    if (exit == 'Y') {
                        isfalg = false;
                    }
            }
        }
    }

    private void addNewCustomer() {
        System.out.println("---------------------添加客户---------------------");
        System.out.print("姓名：");
        String name = CMUtility.readString(10);
        System.out.print("性别：");
        char gender = CMUtility.readChar();
        System.out.print("年龄：");
        int age = CMUtility.readInt();
        System.out.print("电话：");
        String phone = CMUtility.readString(15);
        System.out.print("邮箱：");
        String email = CMUtility.readString(15);

        Customer customer = new Customer(name, gender, age, phone, email);
        boolean istrue = customerList.addCustomer(customer);
        if (istrue) {
            System.out.println("---------------------添加完成---------------------");
        } else {
            System.out.println("----------------记录已满,无法添加-----------------");
        }

    }

    private void modifyCustomer() {
        System.out.println("---------------------修改客户---------------------");
        int number;
        Customer cust;
        for (; ; ) {
            System.out.print("请选择待修改客户编号(-1退出)：");
            number = CMUtility.readInt();
            if (number == -1) {
                return;
            }
            cust = customerList.getCustomer(number - 1);
            if (cust == null) {
                System.out.println("无法找到指定客户！");
            } else {
                break;
            }
        }
        System.out.println("姓名(" + cust.getName() + ")：");
        String name = CMUtility.readString(10, cust.getName());
        System.out.print("性别(" + cust.getGender() + ")：");
        char gender = CMUtility.readChar(cust.getGender());
        System.out.print("年龄(" + cust.getAge() + ")：");
        int age = CMUtility.readInt(cust.getAge());
        System.out.print("电话(" + cust.getPhone() + ")：");
        String phone = CMUtility.readString(15, cust.getPhone());
        System.out.print("邮箱(" + cust.getEmail() + ")：");
        String email = CMUtility.readString(15, cust.getEmail());

        Customer customer=new Customer(name,gender,age,phone,email);
        boolean istrue= customerList.replaceCustomer(number-1,customer);
        if (istrue){
            System.out.println("---------------------修改完成---------------------");
        }else {
            System.out.println("----------无法找到指定客户,修改失败--------------");
        }
    }

    private void deleteCustomer() {
        System.out.println("---------------------删除客户---------------------");
        int number;
        Customer cust;
        for (; ; ) {
            System.out.print("请选择待删除客户编号(-1退出)：");
            number = CMUtility.readInt();
            if (number == -1) {
                return;
            }
            cust = customerList.getCustomer(number - 1);
            if (cust == null) {
                System.out.println("无法找到指定客户！");
            } else {
                break;
            }
        }
        System.out.println("确认是否删除(Y/N)：");
         char why= CMUtility.readChar();
        if (why=='Y'){
            customerList.deleteCustomer(number-1);
            System.out.println("---------------------删除完成---------------------");
        }else {
            return;
        }

    }

    private void listAllCustomers() {
        System.out.println("---------------------------客户列表---------------------------");
        Customer[] custs = customerList.getAllCustomers();
        if (custs.length == 0) {
            System.out.println("该列表里面没有客户");
        } else {
            System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t邮箱");
            for (int i = 0; i < custs.length; i++) {
                System.out.println((i + 1) + "\t" + custs[i].getName() + "\t" +
                        custs[i].getGender() + "\t" + custs[i].getAge() + "\t\t" + custs[i].getPhone() +
                        "\t" + custs[i].getEmail());
            }
        }

        System.out.println("-------------------------客户列表完成-------------------------");
    }

    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.enterMainMenu();
    }


}